//
//  YZLNewsScrollCell.m
//  Car
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 岳明. All rights reserved.
//

#pragma mark - *轮播图*

#import "YZLNewsScrollCell.h"

#import "YZLFocusImgs.h"
#import "YZLFocusListModel.h"
#import "Macro.h"
#import <UIImageView+WebCache.h>

@interface YZLNewsScrollCell ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *modelArray;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIImageView *firstImageView;
@property (nonatomic, strong) UIImageView *lastImageView;

@end

@implementation YZLNewsScrollCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 轮播图
        self.scrollView = [[UIScrollView alloc] init];
        [self.contentView addSubview:self.scrollView];
        // 关闭反弹
        self.scrollView.bounces = NO;
        // 开启页面
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        /**************************************************************************************/
        // 设置轮播图内容
        for (int i = 0; i < 5; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] init];
            [self.scrollView addSubview:imageView];
            imageView.userInteractionEnabled = YES;
            imageView.tag = i + 1;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTap:)];
            [imageView addGestureRecognizer:tap];
        }
        
        // 将数组中的最后一张图复制到轮播图的第一位
        self.firstImageView = [[UIImageView alloc] init];
        [self.scrollView addSubview:self.firstImageView];
        
        
        // 将数组的第一张图复制到轮播图的最后一位
        self.lastImageView = [[UIImageView alloc] init];
        [self.scrollView addSubview:self.lastImageView];
        
        /**************************************************************************************/
        // 标签管理
        self.page = [[UIPageControl alloc] init];
        [self.contentView addSubview:self.page];
        // 未选中设置为白色
        self.page.pageIndicatorTintColor = [UIColor grayColor];
        // 选中是设置为蓝色
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        
        // 开启计时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(playTimer) userInfo:nil repeats:YES];
        
        // 开启用户交互
        self.contentView.multipleTouchEnabled = YES;
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.scrollView.frame = self.contentView.frame;
    // 设置内容视图范围
    if (self.modelArray.count == 0) {
        self.scrollView.contentSize = CGSizeMake(kWidth, 0);
        self.scrollView.contentOffset = CGPointMake(0, 0);
    } else {
        self.scrollView.contentSize = CGSizeMake((self.modelArray.count + 2) * kWidth, 0);
        // 将第二张图片置于第一次显示的位置
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    }
    
    
    for (int i = 0; i < self.modelArray.count; i++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:i + 1];
        imageView.frame = CGRectMake((i + 1) * kWidth, 0, kWidth, self.contentView.frame.size.height);
    }
    self.firstImageView.frame = CGRectMake(0, 0, kWidth, self.contentView.frame.size.height);
    self.lastImageView.frame = CGRectMake(kWidth * self.modelArray.count + kWidth, 0, kWidth, self.contentView.frame.size.height);
    
    // 设置标签控制
    self.page.numberOfPages = self.modelArray.count;
    self.page.frame = CGRectMake(10, self.contentView.frame.size.height * 4 / 5, kWidth / self.modelArray.count, self.contentView.frame.size.height / 5);
    [self.page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)imageViewTap:(UIGestureRecognizer *)tap {
    [self.delegate pushNewsScrollWithNumber:self.scrollView.contentOffset.x];
}



- (void)page:(UIPageControl *)page {
    [UIView animateWithDuration:.35 animations:^{
        [self.scrollView setContentOffset:CGPointMake(page.currentPage * kWidth + kWidth, 0)];
    }];
}

#pragma mark - 计时器事件
- (void)playTimer {
    // 判断偏移量是否到轮播图的最后一位
    if (self.scrollView.contentOffset.x != self.modelArray.count * kWidth + kWidth) {
        // 不是，正常滑动
        [UIView animateWithDuration:.35 animations:^{
            
            self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + kWidth, 0);
        }];
        
    }else {
        // 是，将轮播图的偏移量置于第二位
        self.scrollView.contentOffset = CGPointMake(kWidth, 0);
        
        [UIView animateWithDuration:.35 animations:^{
            self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + kWidth, 0);
        }];
        
    }
    
    // 判断偏移量是否到轮播图的最后一位
    if (self.scrollView.contentOffset.x == self.modelArray.count * kWidth + kWidth) {
        // 是，控制标签变为0
        self.page.currentPage = 0;
    }else {
        // 不是，控制标签根据轮播图的偏移量改变
        self.page.currentPage = self.scrollView.contentOffset.x / kWidth - 1;
    }

}

#pragma mark - 轮播图滑动情况
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(kWidth * self.modelArray.count, 0);
    } else if (scrollView.contentOffset.x == kWidth * self.modelArray.count + kWidth) {
        scrollView.contentOffset = CGPointMake(kWidth, 0);
    }
    
    self.page.currentPage = scrollView.contentOffset.x / kWidth - 1;
}

- (void)setModel:(YZLFocusListModel *)model {
    if (_model != model) {
        _model = model;
    }
    
    _modelArray = model.focusImgs;
    for (int i = 0; i < _modelArray.count; i++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:i + 1];
        YZLFocusImgs *model = [self.modelArray objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.imgURL] placeholderImage:[UIImage imageNamed:@"D0"]];
    }
    
    [self.firstImageView sd_setImageWithURL:[NSURL URLWithString:[[self.modelArray lastObject] imgURL]] placeholderImage:[UIImage imageNamed:@"D0"]];
    [self.lastImageView sd_setImageWithURL:[NSURL URLWithString:[[self.modelArray firstObject] imgURL]] placeholderImage:[UIImage imageNamed:@"D0"]];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
